package com.briup.jz.service.impl;
import com.briup.jz.bean.Order;
import com.briup.jz.bean.OrderExample;
import com.briup.jz.dao.OrderMapper;
import com.briup.jz.service.IOrderService;
import com.briup.jz.utils.CustomerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class OrderImpl implements IOrderService {
    @Resource
    private OrderMapper orderMapper;
//    保存或咋修改订单
    @Override
    public void saveOrUpdate(Order order) throws CustomerException {
//        判断用户是否输入订单id
        if(order.getId()==null){
            orderMapper.insert(order);
        }else {
            orderMapper.updateByPrimaryKey(order);
        }
    }
//根据主键id删除订单
    @Override
    public void deleteById(Long id) throws CustomerException {
        Order order=orderMapper.selectByPrimaryKey(id);
        if (order==null){
            throw  new CustomerException("没有这个订单");
        }
        orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Order> query(Long id, Long customerid, Long employeeid) {
        OrderExample example=new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        if (id!=null){
            criteria.andIdEqualTo(id);
        }
        if (customerid!=null){
            criteria.andEmployeeIdEqualTo(customerid);
        }
        if (employeeid!=null){
            criteria.andIdEqualTo(employeeid);
        }
        return orderMapper.selectByExample(example);
    }
	
	@Override
	public void cancelSendOrder(Long id) throws CustomerException{
		// TODO Auto-generated method stub
		Order order=orderMapper.selectByPrimaryKey(id);
		if(order==null) {
			throw new CustomerException("没有这个订单");
		}
//		派单失败后重新进入待派单状态
		order.setStatus("已取消");
		saveOrUpdate(order);
	}
	@Override
	public void confirmOrder(Long id) {
		// TODO Auto-generated method stub
		Order order=orderMapper.selectByPrimaryKey(id);
		if(order==null) {
			throw new CustomerException("没有这个订单");
		}
//		确认订单后进入待服务状态
		order.setStatus("待服务");
		saveOrUpdate(order);
	}
	@Override
	public void rejectOrder(Long id) {
		// TODO Auto-generated method stub
		Order order=orderMapper.selectByPrimaryKey(id);
		if(order==null) {
			throw new CustomerException("没有这个订单");
		}
//		拒绝接单后进入待接单状态
		order.setStatus("待接单");
		saveOrUpdate(order);
	}
	@Override
	public void competeorder(Long id) {
		// TODO Auto-generated method stub
		Order order=orderMapper.selectByPrimaryKey(id);
		if(order==null) {
			throw new CustomerException("没有这个订单");
		}
//		服务结束后进入已服务状态
		order.setStatus("已服务");
		saveOrUpdate(order);
	}
	@Override
	public void sendOrder(Long id) {
		// TODO Auto-generated method stub
		Order order=orderMapper.selectByPrimaryKey(id);
		if(order==null) {
			throw new CustomerException("没有这个订单");
		}
//		服务结束后进入已服务状态
		order.setStatus("正在派单");
		saveOrUpdate(order);
	}
	@Override
	public void takeOrder(Long id) {
		// TODO Auto-generated method stub
		Order order=orderMapper.selectByPrimaryKey(id);
		if(order==null) {
			throw new CustomerException("没有这个订单");
		}
//		服务结束后进入已服务状态
		order.setStatus("已接单");
		saveOrUpdate(order);
	}
	@Override
	public List<Order> query(Long id) {
		// TODO Auto-generated method stub
		OrderExample orderExample=new OrderExample();
		if (id!=null){
			orderExample.createCriteria().andIdEqualTo(id);
		}
		List<Order> orders=new ArrayList<>();
		orders=orderMapper.selectByExample(orderExample);
		return orders;
	}
}
